package com.gamebox.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import com.gamebox.javaBean.User;
import com.gamebox.util.DBDriver;
import com.gamebox.util.DBTool;

public class UserDao {

	public boolean addUser(User user) {

		String sql = "INSERT INTO user(username,password,type,nickName,registerTime,groupId) VALUES(?,?,?,?,?,?)";
		Connection conn = DBDriver.getConnection();
		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, user.getName());
			pstmt.setString(2, user.getPassword());
			pstmt.setInt(3,user.getType());
			pstmt.setString(4, user.getNickName());
			pstmt.setDate(5, new java.sql.Date(user.getRegisterTime().getTime()));
			pstmt.setInt(6,user.getGroupId());
			return pstmt.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

	public User login(String name, String password) {
		User user = null;
		String sql = "select  * from user where username = '" + name + "' and password = '" + password
				+ "' ";
		DBTool dbTool = new DBTool();
		ResultSet result = dbTool.excuteSelect(sql);
		if (result == null) {
			return user;
		}
		try {
			while (result.next()) {
				int id = result.getInt("id");
				String username = result.getString("username");
				String upassword = result.getString("password");
				int type = result.getInt("type");
				String nickName = result.getString("nickname");
				Date registerTime = result.getDate("registerTime");
				int groupId = result.getInt("groupId");
				user = new User(id,username,upassword,type,nickName,groupId,registerTime);
				
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		return user;

	}

	public User getUserByName(String name) {
		User user = null;
		String sql = "select * from user where username = '" + name + "'";
		DBTool dbTool = new DBTool();
		ResultSet result = dbTool.excuteSelect(sql);
		if (result == null) {
			return user;
		}
		try {
			while (result.next()) {
				int id = result.getInt("id");
				String username = result.getString("username");
				String upassword = result.getString("password");
				int type = result.getInt("type");
				String nickName = result.getString("nickname");
				Date registerTime = result.getDate("registerTime");
				int groupId = result.getInt("groupId");
				user = new User(id,username,upassword,type,nickName,groupId,registerTime);
				
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		return user;

	}
	
	public ArrayList<User> getAllUsers(){
		ArrayList<User> users = new ArrayList<User>();
		String sql = "select * from user";
		DBTool dbTool = new DBTool();
		ResultSet result = dbTool.excuteSelect(sql);
		if (result == null) {
			return users;
		}
		try {
			while (result.next()) {
				int id = result.getInt("id");
				String username = result.getString("username");
				String upassword = result.getString("password");
				int type = result.getInt("type");
				String nickName = result.getString("nickname");
				Date registerTime = result.getDate("registerTime");
				int groupId = result.getInt("groupId");
				User user = new User(id,username,upassword,type,nickName,groupId,registerTime);
				users.add(user);
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		return users;
		
	}
	public ArrayList<User> getAllUsersById(){
		ArrayList<User> users = new ArrayList<User>();
		String sql = "select * from user order by id asc";
		DBTool dbTool = new DBTool();
		ResultSet result = dbTool.excuteSelect(sql);
		if (result == null) {
			return users;
		}
		try {
			while (result.next()) {
				int id = result.getInt("id");
				String username = result.getString("username");
				String upassword = result.getString("password");
				int type = result.getInt("type");
				String nickName = result.getString("nickname");
				Date registerTime = result.getDate("registerTime");
				int groupId = result.getInt("groupId");
				User user = new User(id,username,upassword,type,nickName,groupId,registerTime);
				users.add(user);
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		return users;
		
	}
	public ArrayList<User> getAllUsersByName(){
		ArrayList<User> users = new ArrayList<User>();
		String sql = "select * from user order by username asc";
		DBTool dbTool = new DBTool();
		ResultSet result = dbTool.excuteSelect(sql);
		if (result == null) {
			return users;
		}
		try {
			while (result.next()) {
				int id = result.getInt("id");
				String username = result.getString("username");
				String upassword = result.getString("password");
				int type = result.getInt("type");
				String nickName = result.getString("nickname");
				Date registerTime = result.getDate("registerTime");
				int groupId = result.getInt("groupId");
				User user = new User(id,username,upassword,type,nickName,groupId,registerTime);
				users.add(user);
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		return users;
		
	}

}
